home *** CD-ROM | disk | FTP | other *** search
- package icontrols.MaskedEdit;
-
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class FormatText {
- public static final int NUMERIC = 1;
- public static final int DATETIME = 2;
- public static final int STRING = 3;
- private DecimalFormat decimalFormat;
- private String format;
- private int type;
-
- public String reformatText(String text) throws IllegalArgumentException {
- String result = "";
- if (this.type == 3) {
- return text;
- } else {
- if (this.format.length() != 0 && text.length() != 0) {
- if (this.type == 1) {
- try {
- DecimalFormat df = this.decimalFormat;
- Double num = Double.valueOf(text);
- if (num == null) {
- throw new IllegalArgumentException("unable to parse text");
- }
-
- result = ((NumberFormat)df).format(num);
- } catch (Exception var7) {
- throw new IllegalArgumentException("unable to parse text");
- }
- } else {
- for(int i = 0; i <= 3; ++i) {
- SimpleDateFormat sdf;
- switch (i) {
- case 0:
- sdf = (SimpleDateFormat)DateFormat.getDateInstance(3);
- break;
- case 1:
- sdf = (SimpleDateFormat)DateFormat.getTimeInstance(3);
- break;
- case 2:
- sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance(3, 3);
- break;
- default:
- throw new IllegalArgumentException("unable to parse text");
- }
-
- try {
- Date date = ((DateFormat)sdf).parse(text);
- if (date != null) {
- sdf = new SimpleDateFormat(this.format);
- result = ((DateFormat)sdf).format(date);
- break;
- }
- } catch (Exception var8) {
- }
- }
- }
- }
-
- return new String(result);
- }
- }
-
- public String toString() {
- return "{format=\"" + this.format + "\",type=" + this.type + "}";
- }
-
- public FormatText() {
- this("", 1);
- }
-
- public FormatText(String format, int type) {
- this.decimalFormat = null;
- this.setFormat(format, type);
- }
-
- public void setFormat(String format, int type) throws IllegalArgumentException {
- if (type != 1 && type != 2 && type != 3) {
- throw new IllegalArgumentException("invalid type");
- } else {
- this.format = format;
- this.type = type;
- this.decimalFormat = new DecimalFormat(format);
- }
- }
-
- public String getFormatString() {
- return new String(this.format);
- }
-
- public String reformatDouble(double value) throws IllegalArgumentException {
- return this.decimalFormat.format(value);
- }
-
- public int getFormatType() {
- return this.type;
- }
- }
-